import random
import re

from django.core.paginator import Paginator
from rest_framework.response import Response
from rest_framework.views import APIView

from utils.comm import send_message, get_redis
from .models import *
from .ser import NewsSer


class NewAPIView(APIView):
    def post(self, request):
        data = NewsSer(data=request.data)
        if data.is_valid():
            data.save()
            return Response({'code': 200, 'msg': '添加成功'})
        else:
            print(data.errors)
            return Response({'code': '10010'})

    def get(self, request):
        news = News.objects.all()
        p = Paginator(news, 2)
        cpage = request.GET.get('page')
        newsR = p.page(cpage)
        ser = NewsSer(newsR, many=True)
        return Response({'code': 200, 'list': ser.data, 'tpage': p.num_pages})


# 注册接口
class Register(APIView):
    def post(self, request):
        username = request.data.get('username')
        # 验证用户名是否为空
        if not username:
            return Response({'code': '10010', 'msg': '用户名不能为空'})
        # 格式是否正确
        name = re.match('\w{5,10}$', username)
        if not name:
            return Response({'code': '10011'})
        # 是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({'code': '10012'})
        # 验证密码是否为空
        password = request.data.get('password')
        if not password:
            return Response({'code': '10013', 'msg': '密码不能为空'})
        # 密码格式是否正确
        pd = re.match('\w{8,20}$', password)
        if not pd:
            return Response({'msg': '密码格式不对', 'code': '10014'})
        # 手机号
        mobile = request.data.get('mobile')
        # 验证用户名是否为空
        if not mobile:
            return Response({'code': '10015', 'msg': '手机号不能为空'})
        # 格式是否正确
        phone = re.match('^1[3-9]\d{9}$', mobile)
        if not phone:
            return Response({'code': '10016'})
        # 是否唯一
        sloe = User.objects.filter(mobile=mobile).first()
        if sloe:
            return Response({'code': '10017'})
        # 验证码验证newcode，redis取根据手机号
        smscode = request.data.get('smscode')
        conn = get_redis()
        code = conn.get(mobile)
        if code:
            code = str(code,encoding='utf8')
        if smscode != code:
            return Response({'code': '10018'})
        return Response({'code': 200, 'msg': '注册成功'})


class Test(APIView):
    def get(self, request):
        # send_message('18530561795', '123', 1)
        get_redis()
        return Response('ok')


class SendSms(APIView):
    def post(self,request):
        mobile = request.data.get('mobile')
        codesms = random.randint(100000,999999)
        res = send_message(mobile,str(codesms),1)
        code = 10010
        if res:
            code = 200
            conn = get_redis()
            conn.setex(mobile, 60, codesms)
        return Response({'code': code})
